<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%@ include file="common.jsp"%>

<script type="text/javascript">
	var activeTheme="dhx_skyblue";
	var tree = new dhtmlXTreeObject("treeBox","100%","100%",0);
	function initTree(json){
		tree.setImagePath("${contextPath }/resources/assets/plugins/dhtmlxTree/imgs/csh_"+activeTheme+"/");
		tree.setIconsPath("${contextPath }/resources/assets/plugins/dhtmlxTree/imgs/csh_"+activeTheme+"/");
		tree.enableCheckBoxes(true);
		tree.enableThreeStateCheckboxes(true);
	  	tree.loadJSONObject(json, function(){
	  		tree.openAllItems(0);
	  	});
	};
	
//进来后要做的事情

	$(function(){
		buttonsBlur();

		var type = '${type}';
		$('#btnBack').click(function(event){
			$("#main-content").load('${contextPath }/admin/toRoleManager'); //+(type == 0 ? 'main' : 'single')
			event.preventDefault();
		});

		$('#btnSelectAll').click(function(){
			tree.setCheck(0,true);
		});
		$('#btnUnSelectAll').click(function(){
			tree.setCheck(0,false);
		});

		$('#btnSubmit').click(function(){
			var idList = [];
			var nodes = tree.getAllChecked();
			//if(!nodes || nodes.length == 0) return;

			nodes = nodes.split(",");
			for(var i = 0; i < nodes.length; i++) {
				var node = nodes[i];
				var index = node.indexOf('-');
				if(index > 0) {
					var id = node.substring(0, index);
					var code = node.substring(index+1, node.length);
					if(code.length >= 8) {
						idList.push(id);
					}
				}
			}

			showLoading('#btnSubmit');
			// var data = {roleId:'${roleId}', actions:idList.join(',')};
			var rId = $('#roleId').val();
			var fs = getCheckedFunction();
			var data = {roleId:rId,functions:fs};
			$.post('${contextPath }/role/permission/add',data, function(rs){
				hideLoading('#btnSubmit');
				if(rs.code == SUCCESS) {
					$.scojs_message('角色授权成功!', $.scojs_message.TYPE_OK);
				}else {
	  				$.scojs_message('角色授权失败!', $.scojs_message.TYPE_ERROR);  
	  			}
			});
		});

		$('.imgLoading').attr('hidden', false);

//发起请求
		$.get('${contextPath }/function/byUserId?roleId=${param.roleId}&type=${param.type}', function(rs){
			var parents = [];
			var functions = rs.functions;
			//var actions = rs.actions;
			for(var i = 0; i < functions.length; i++) {
				var func = functions[i];
				if(func.pCode == 0 || func.pCode == '0') {
					func.id = func.id+'-'+func.code;
					//func.im0 = func.icon+".png";
					func.text = func.title;
					func.item = [];
					parents.push(func);
				}
			}
			
			for(var i = 0; i < parents.length; i++) {
				var parent = parents[i];
				for(var j = 0; j < functions.length; j++) {
					var r = functions[j];
					if(r.pCode == parent.code) {
						r.id = r.id+'-'+r.code;
						r.text = r.title;
						//r.im0 = r.icon+".png";
						r.item = [];
						parent.item.push(r);
					}
				}
			}

			// for(var i = 0; i < actions.length; i++) {
			// 	var action = actions[i];
			// 	add(action);
			// }

			function add(c) {
				var _break = false;
				for(var i = 0; i < parents.length; i++) {
					var childs = parents[i].item;
					for(var j = 0; j < childs.length; j++) {
						var r = childs[j];
						if(r.code == c.fCode) {
							c.id = c.id + '-' + c.code;
							c.text = c.name;
							c.im0 = 'user.png';
							c.checked = c.checked > 0;
							r.item.push(c);
							_break = true;break;
						}
					}
					if (_break) break;
				}
			}
			
			var json = {id:0, text:'权限管理',item:parents};
			initTree(json);
			
			$('.imgLoading').attr('hidden', true);
		});
	});

//1-1001,3-100101,4-100102,2-1002,5-100201

	function getCheckedFunction() {

	    var nodes = tree.getAllChecked();
	    var nodeArr = nodes.split(",");
	    var node = null;
	    var data = "";
	    for(var i = 0;i<nodeArr.length;i++) {
	    	node = nodeArr[i];
	    	var tempArr = node.split('-');
	    	var tempValue = tempArr[1];
	    	if(i==0){
	    		data += tempValue;
	    	}else {
	    		data += ","+tempValue;
	    	}
	    }
	    return data;
	}
</script>

<button type="button" id="btnBack" class="button  button-rounded button-flat-primary" style="float:left;">
	<i class="fa fa-arrow-left"></i>点击返回</button>

<div class="container">
	<input type="hidden" id="roleId"/>
	<div class="row">
		<fieldset>
			<legend>
				<c:choose>
		   			<c:when test="${type == 0}">总店&nbsp;——</c:when>
		   			<c:otherwise>分店&nbsp;—</c:otherwise>
				</c:choose>
 			<font color=red><label id="roleName"></label></font>&nbsp;权限分配<img class="imgLoading" src="resources/assets/img/loading_small.gif" style="float:left;" /></legend>
			
			<form id="roleForm" class="form-horizontal">
				<div class="form-group">
					<div id="treeBox" style="width:400px;height:600px;border:1px solid #000"></div>
				</div>
				<div class="form-group">
					<div class="col-lg-8 col-lg-offset-1">
						<button type="button" id="btnSubmit" class="btn btn-primary"><i id="imgLoading" class="fa fa-spinner fa-spin" style="display:none"></i><i class="fa fa-floppy-o"></i> 保存</button>
						<button type="button" id="btnSelectAll" class="btn btn-default"><i class="fa fa-check-square-o"></i> 全选</button>
						<button type="button" id="btnUnSelectAll" class="btn btn-warning"><i class="fa fa-undo"></i> 全不选</button>
					</div>
				</div>
			</form>
		</fieldset>
	</div>
</div>




